package com.ngc.mall.service.impl;

import com.ngc.mall.dto.FileInfoDTO;
import com.ngc.mall.service.ICosService;
import com.ngc.mall.vo.CosConfigVO;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.model.COSObject;
import com.qcloud.cos.model.GetObjectRequest;
import com.qcloud.cos.region.Region;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class CosServiceImpl implements ICosService {

    @Value("${cos.secretId}")
    private String secretId;

    @Value("${cos.secretKey}")
    private String secretKey;

    @Value("${cos.bucketName}")
    private String bucketName;

    /**
     * 获取文件对象信息
     */
    @Override
    public FileInfoDTO getFormalObject(String enclosure) {
        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
        // 2 设置 bucket 的地域, COS 地域的简称请参见 https://cloud.tencent.com/document/product/436/6224
        // clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。
        Region region = new Region("ap-guangzhou");
        ClientConfig clientConfig = new ClientConfig(region);

        clientConfig.setHttpProtocol(HttpProtocol.https);
        // 3 生成 cos 客户端。
        COSClient cosClient = new COSClient(cred, clientConfig);
        GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, enclosure);
        COSObject object = cosClient.getObject(getObjectRequest);
        System.out.println(object);
        FileInfoDTO fileInfoDTO = new FileInfoDTO();
        fileInfoDTO.setFileSize(object.getObjectMetadata().getContentLength());
        fileInfoDTO.setFileUrl(object.getObjectContent().getHttpRequest().getURI().toString());
        return fileInfoDTO;

    }

    /**
     * 获取文件对象URL
     */
    @Override
    public String getObjectUrl(String enclosure){
        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
        // 2 设置 bucket 的地域, COS 地域的简称请参见 https://cloud.tencent.com/document/product/436/6224
        // clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。
        Region region = new Region("ap-guangzhou");
        ClientConfig clientConfig = new ClientConfig(region);

        clientConfig.setHttpProtocol(HttpProtocol.https);
        // 3 生成 cos 客户端。
        COSClient cosClient = new COSClient(cred, clientConfig);
        GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, enclosure);
        COSObject object = cosClient.getObject(getObjectRequest);
        String objectUrl=object.getObjectContent().getHttpRequest().getURI().toString();
        return objectUrl;
    }

    /**
     * 返回前端secretid,secretKey
     */
    @Override
    public CosConfigVO getCosSecretConfig() {
        CosConfigVO vo = CosConfigVO
                .builder()
                .secretId(secretId)
                .secretKey(secretKey)
                .build();
        return vo;
    }

}
